﻿<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            margin: 100px auto 0;
        }

        .wrap h1 {
            text-align: center;
        }

        .wrap div {
            width: 400px;
            height: 300px;
            background: pink;
            font-size: 30px;
            text-align: center;
            line-height: 300px;
        }
    </style>
    <script src="./jquery-1.11.1.js"></script>
    <script>
        $(window).ready(function () {

            $(document).on('keydown',function(event){

                $('#keyCodeSpan').text(event.keyCode)
                switch (event.keyCode){
                 case 82:
                    $('#bgChange').css('background','red')
                    break
                 case 89:
                    $('#bgChange').css('background','yellow')
                    break 
                 case 80:
                    $('#bgChange').css('background','purple') 
                    break 
                case 66:
                    $('#bgChange').css('background','purple') 
                    break 
                    case 83:
                    $('#bgChange').css('background','purple') 
                    break 
                    case 71:
                    $('#bgChange').css('background','purple') 
                    break 
                    case 79:
                    $('#bgChange').css('background','purple')  
                    break 
                    default:
                        $('#keyCodeSpan').text('无此键位')  
                        $('#bgChange').css('backgroundColor','pink')
                }
            })
            
        })
    </script>
</head>
<body>

    <div class="wrap">
        <h1>按键改变颜色</h1>
        <div id="bgChange">
            keyCode为：
            <span id="keyCodeSpan"></span>
        </div>
    </div>

</body>
</html>
